//Create by zhongxiang.huang
$(function () {
    
    $MB.initTpvMeetingElement();
    
    var settings = {
        url: ctx + "appupdate/list/3",
        pageSize: 10,
        clickToSelect: false,
        queryParams: function (params) {
            var startDate =$("input[name='app-filter-start-date-link']").val();
            var endDate = $("input[name='app-filter-end-date-link']").val();

            var hasStartDateSelected = startDate != '';
            var hasEndDateSelected = endDate != '';

            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                startDate: (hasStartDateSelected ? new Date(startDate) : undefined),
                endDate: (hasEndDateSelected ? new Date(endDate) : undefined),
            };
        },
        columns: [{
            checkbox: true
        }, {
            field: 'id',
            visible: false
        }, {
            field: 'name',
            title: 'name',
            cellStyle: {
                css: {                    
                    'text-align':'center',
                }
            }
        }, {
            field: 'versionCode',
            title: 'versionCode',
            cellStyle: {
                css: {                    
                    'text-align':'center',
                }
            }
        }, {
            field: 'publishTime',
            title: 'publishTime',
            cellStyle: {
                css: {                    
                    'text-align':'center',
                }
            }            
        }, {
            field: 'downPath',
            title: 'downPath',
            cellStyle: {
                css: {                    
                    'text-align':'center',
                }
            }            
        }]
    };

    $MB.initTable('IOSIPadAppTable', settings);
	
	$("#file-upload .finish").click(function () {
		var name = $("input[name='name']").val();
		var downPath = $("input[name='downPath']").val();
		var versionCode = $("input[name='versionCode']").val();
		var publishStatus = $("select[name='publishStatus']").val();		
		var data = {
            "name": name,
            "downPath":downPath,
            "versionCode":versionCode,
            "publishStatus":publishStatus,
            "fileType":3
        };

		$(this).attr("disabled",true);
        $.post(ctx + "appupdate/add", data, function (r) {
            if (r.code === 0) {
                closeModal();
                $MB.n_success(r.msg);
                $MB.refreshTable("IOSIPadAppTable");
            } else {
                $MB.n_danger(r.msg);
            }
            $("#file-upload .finish").removeAttr("disabled");
        });
       
	});
	
	$("#IOSIpad-edit .finish").click(function () {
		var appId = $("#IOSIPadEditForm").attr("app-id");
		var name = $("#edit-name").val();
		var downPath = $("#edit-down-path").val();
		var versionCode = $("#edit-version-code").val();
		$(this).attr("disabled",true);
		
		var data = {
			"fileType":3,
            "name": name,
            "downPath": downPath,
            "versionCode": versionCode
        };
		writeObj(data);
		$.post(ctx + "appupdate/edit/" + appId, data, function (r) {
            if (r.code === 0) {
            	$MB.closeAndRestModal("IOSIpad-edit");
            	$("#IOSIpad-edit .finish").removeAttr("disabled");
                $MB.n_success(r.msg);
                $MB.refreshTable("IOSIPadAppTable");
            } else {
                $MB.n_danger(r.msg);
            }
            	
            $("#IOSIpad-edit .finish").removeAttr("disabled");
        });
	});

    $("#app-filter-start-date").on("change", search);
    $("#app-filter-end-date").on("change", search);
});

function writeObj(obj){ 
    var description = ""; 
    for(var i in obj){ 
        var property=obj[i]; 
        description+=i+" = "+property+"\n"; 
    } 
    console.log("*******************writeObj***************"+description);
}

function addNewVersion(){
	var $form = $('#file-upload');
    $form.modal();
}

function edit() {
    var selected = $("#IOSIPadAppTable").bootstrapTable('getSelections');
    var selected_length = selected.length;

    if(!selected_length) {
        $MB.n_warning("请选择要编辑的应用");
        return;
    }
    if(selected_length > 1) {
        $MB.n_warning("只支持单个应用编辑");
        return;
    }

    doEtit(selected[0].id);
}

function doEtit(appId){
	$.get(ctx + "appupdate/edit/" + appId, function(r) {
        if(r.code == 0) { 
        	var app = r.msg;

            $appEditForm = $("#IOSIPadEditForm")
            $appEditForm.attr("app-id", app.id);
            
            $("#edit-name").val(app.name);
    		$("#edit-down-path").val(app.downPath);
    		$("#edit-version-code").val(app.versionCode);
           
            $('#IOSIpad-edit').modal();
        } else {
            $MB.n_danger(r.msg);
        }
    });
}

function downLoadPlugin(){
	var selected = $("#IOSIPadAppTable").bootstrapTable('getSelections');
    var selected_length = selected.length;

    if(!selected_length) {
        //$MB.n_warning($MB.i18n('meeting.tips.edit.select.none'));
    	$MB.n_warning("请选择要下架的插件");
        return;
    }  
}

function search() {
    $MB.refreshTable('IOSIPadAppTable');
}


function refresh() {
    $(".IOSIPad-app-table-form input[type=hidden]").val('');
    $(".IOSIPad-app-table-form")[0].reset();

    $MB.refreshTable('IOSIPadAppTable');
}

function closeModal() {
    $("#file-upload .finish").removeAttr("disabled");
    $MB.closeAndRestModal("file-upload");
}



